clc;
clear all;
close all;

%% Load input image for edge detection algorithms
imgRGB = imread('retina3.jpg');
img = rgb2gray(imgRGB);
[rows cols] = size(img);

%% Canny Edge Detection
% %% Adapt sigma
% sigmas = [0.1 0.5 1 1.5 2 2.5 3 5 10];
sigmas = [1 2 3];
hsizes = ceil(3 * sigmas);
figure;
subplot(2,2,1); imshow(img);
% handle = tight_subplot(3,3,[.07 .01],[.1 .01],[.1 .01]);
for i = 1:length(sigmas)
    canny_edge_img = ecen5283_edge_detect_canny(img, sigmas(i), hsizes(i), 'fast');
    subplot(2,2,i+1);
    imshow(canny_edge_img);
    title([' sigma =', num2str(sigmas(i)), ' ; size = ', num2str(2*hsizes(i)+1)]);
end


%% The best
sigmas = 2;
hsizes = ceil(3 * sigmas);
figure;
handle = tight_subplot(3,2,[.05 .01],[.03 .01],[.01 .01]);
axes(handle(1)); imshow(imgRGB); title('Original image');
axes(handle(2)); imshow(img);title('Gray image');
t1 = cputime;
[canny_edge_img color_edge_img mg] = ecen5283_edge_detect_canny(img, sigmas, hsizes, 'interpolation');
t2 = cputime;
duration = t2 - t1
axes(handle(3)); imshow(mg, []); title('Gradient Magnitude');
axes(handle(4)); imshow(canny_edge_img); title([' sigma =', num2str(sigmas), ' ; size = ', num2str(2*hsizes+1)]);
axes(handle(5)); imshow(color_edge_img); title('Color edge image');
axes(handle(6));
canny_rgb = repmat(uint8(255*canny_edge_img),[1 1 3]);
si_img = imadd(imgRGB, canny_rgb);
imshow(si_img);title('Superimposed image');
